import 'package:get/get.dart';
import 'package:largefriends/config/lf_route.dart';
import 'package:largefriends/entity/entities/lf_location_entity.dart';
import 'package:largefriends/entity/entities/lf_match_filter_entity.dart';
import 'package:largefriends/service/lf_auth_service.dart';

import '../../../service/manager/lf_search_filter_helper.dart';

toLfSearchFilter() {
  Get.toNamed(LFRoute.pathLfSearchFilter);
}

class LfSearchFilterLogic extends GetxController {
  RxInt gender = 3.obs;
  RxInt minAge = 19.obs;
  RxInt maxAge = 99.obs;
  Rx<LfLocationEntity?> location = Rx(null);

  late LfMatchFilterEntity matchFilterEntity;
  LfAuthService authService = Get.find();

  @override
  void onInit() {
    matchFilterEntity = LfSearchFilterHelper().getSearchFilter();
    gender.value = matchFilterEntity.gender;
    if (matchFilterEntity.minAge < 19) {
      minAge.value = 19;
    } else if (matchFilterEntity.minAge >= 96) {
      minAge.value = 96;
    } else {
      minAge.value = matchFilterEntity.minAge;
    }

    if (matchFilterEntity.maxAge <= minAge.value) {
      maxAge.value = 99;
    } else {
      maxAge.value = matchFilterEntity.maxAge;
    }
    location.value = matchFilterEntity.locationEntity;
    super.onInit();
  }

  bool get enable {
    return (gender.value > 0) &&
        ((gender.value != matchFilterEntity.gender) ||
            (location.value != matchFilterEntity.locationEntity) ||
            (minAge.value != matchFilterEntity.minAge) ||
            (maxAge.value != matchFilterEntity.maxAge));
  }

  save() {
    LfSearchFilterHelper().saveMatchFilter(LfMatchFilterEntity(
      gender: gender.value,
      minAge: minAge.value,
      maxAge: maxAge.value,
      locationEntity: location.value,
    ));
    Get.back();
  }
}
